<template>
  <div :class="{ 'home-module__banner-dropdown container': categoryPosition == 1 }">
    <dropdown v-if="categoryPosition == 1"></dropdown>
    <swiper
      class="home-module__banner"
      :class="`banner-list-swiper-${moduleIndex}`"
      :options="bannerSwiperOption"
    >
      <swiper-slide
        v-for="(item, index) in datas.list"
        :key="index"
        @click.native="clickBanner(item)"
      >
        <img :src="item.url" />
      </swiper-slide>
      <div class="swiper-pagination swiper-pagination-white" slot="pagination"></div>
      <div class="swiper-button-prev swiper-button-white" slot="button-prev"></div>
      <div class="swiper-button-next swiper-button-white" slot="button-next"></div>
    </swiper>
  </div>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
import { mapGetters } from 'vuex'
import dropdown from './dropdown'
export default {
  name: 'Banner',
  components: {
    Swiper,
    SwiperSlide,
    dropdown
  },
  props: {
    datas: {
      type: Object,
      default: () => {}
    },
    moduleIndex: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      bannerSwiperOption: {
        slidesPerView: 1,
        spaceBetween: 0,
        loopAdditionalSlides: 0,
        loop: true,
        autoplay: {
          delay: 5000,
          disableOnInteraction: false
        },
        pagination: {
          el: `.banner-list-swiper-${this.moduleIndex} .swiper-pagination`,
          clickable: true
        },
        navigation: {
          nextEl: `.banner-list-swiper-${this.moduleIndex} .swiper-button-next`,
          prevEl: `.banner-list-swiper-${this.moduleIndex} .swiper-button-prev`
        }
      }
    }
  },
  computed: {
    ...mapGetters({
      categoryPosition: 'projectSetting/getGlobCategoryPositionSwitch'
    })
  },
  methods: {
    clickBanner(item) {
      if (item.type == 1 && item.course_id) {
        this.$router.push({
          path: `/course/${item.course_id}`
        })
      } else if (item.type == 2 && item.href) {
        window.open(item.href, '_blank')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.home-module__banner {
  margin-bottom: 30px;
  cursor: pointer;
  .swiper-slide {
    padding-top: 500px;
    img {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      object-fit: cover;
      vertical-align: top;
    }
    img[src=''],
    img:not([src]) {
      visibility: hidden;
    }
  }
  .swiper-button-prev {
    left: 50%;
    margin-left: -600px;
  }
  .swiper-button-next {
    right: 50%;
    margin-right: -600px;
  }
  .swiper-button-prev,
  .swiper-button-next {
    opacity: 0;
    outline: none;
    width: 40px;
    height: 40px;
    margin-top: calc(-1 * 26px / 2);
    transition: opacity 0.2s;
    background: rgba(48, 49, 51, 0.2);
    border-radius: 100%;
    &:after {
      font-size: 18px;
    }
    &:hover {
      background: rgba(48, 49, 51, 0.3);
    }
  }
  ::v-deep .swiper-pagination-bullet {
    width: 30px;
    height: 3px;
    background: #ffffff;
    opacity: 0.3;
    border-radius: 0;
  }
  ::v-deep .swiper-pagination-bullet-active {
    opacity: 1;
  }
  .swiper-button-prev.swiper-button-disabled,
  .swiper-button-next.swiper-button-disabled {
    pointer-events: auto;
  }
  &:hover {
    .swiper-button-prev,
    .swiper-button-next {
      opacity: 1;
    }
  }
}
.home-module__banner-dropdown {
  display: flex;
  margin-top: 12px;
  margin-bottom: 30px;
  .home-module__banner {
    flex: 1;
    margin-left: 12px;
    margin-bottom: 0px;
    .swiper-button-prev {
      left: 20px;
      margin-left: 0;
    }
    .swiper-button-next {
      right: 20px;
      margin-right: 0;
    }
  }
}
</style>
